﻿using System.Collections.Generic;
using SERVICES;
using SERVICES.Services;

namespace WMILK_MVC4.Models
{
    public class CatalogModel
    {
        public string CatalogName { get; set; }
        public int CatalogId {get; set;}
        public List<SubCatalogModel> SubCatalogs { get; set; }
        public List<ProductModel> Products { get; set; }

        /// <summary>
        /// Create CatalogModel by CATALOG and get SubCatalog list for this CATALOG
        /// </summary>
        /// <param name="catalog">CATALOG from DataBase</param>
        public CatalogModel(CATALOG catalog)
        {
            CatalogId = catalog.CT_ID;
            CatalogName = catalog.CT_NAME;
        }

        public CatalogModel(string catalogName, int catalogId)
        {
            CatalogId = catalogId;
            CatalogName = catalogName;
        }
        /// <summary>
        /// get products list for this catalog
        /// </summary>
        public void InitializeProducts()
        {
            Products = new List<ProductModel>();
            foreach (var product in ProductServices.GetAllProductsByCatalogId(CatalogId))
            {
                var newProductDetailModel = new ProductModel(product);
                Products.Add(newProductDetailModel);
            }
        }

        /// <summary>
        /// get products list for this catalog with a amount
        /// </summary>
        public void InitializeProducts(int numberOfProducts)
        {
            Products = new List<ProductModel>();
            foreach (var product in ProductServices.GetProductsListByCatalogById(CatalogId, numberOfProducts))
            {
                var newProductDetailModel = new ProductModel(product);
                Products.Add(newProductDetailModel);
            }

        }

        /// <summary>
        /// get SubCatalogs list for this catalog
        /// </summary>
        public void InitializeSubCatalogs()
        {
            SubCatalogs = new List<SubCatalogModel>();
            foreach (var item in SubCatalogService.GetSubCatalogListByCatalogId(this.CatalogId))
            {
                var newSubCatalogModel = new SubCatalogModel(item);
                SubCatalogs.Add(newSubCatalogModel);
            }
        }

        /// <summary>
        /// Add subcatalog into SubcatalogList by name and id
        /// </summary>
        /// <param name="subCatalogName"></param>
        /// <param name="subCatalogID"></param>
        public void AddSubCatalog(string subCatalogName, int subCatalogId)
        {
            var subCatalogToAdd = new SubCatalogModel(subCatalogName, subCatalogId);
            SubCatalogs.Add(subCatalogToAdd);
        }

        /// <summary>
        /// Add SubCatalog into SubCatalogList
        /// </summary>
        /// <param name="subCatalogToAdd"></param>
        public void AddSubCatalog(SubCatalogModel subCatalogToAdd)
        {
            SubCatalogs.Add(subCatalogToAdd);
        }


        /// <summary>
        /// get or set SubCatalogModel by index
        /// </summary>
        /// <param name="index">index of SubCatalog</param>
        /// <returns></returns>
        public SubCatalogModel this[int index]
        {
            get 
            {
                if (index >= 0 && index <= SubCatalogs.Count)
                    return SubCatalogs[index];
                return null;
            }
            set 
            {
                if (index >= 0 && index <= SubCatalogs.Count)
                    SubCatalogs[index] = value;
            }
        }
    }
}